package com.baichuan.zntx.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.baichuan.common.idempotent.annotation.RepeatSubmit;
import com.baichuan.common.log.annotation.Log;
import com.baichuan.common.web.core.BaseController;
import com.baichuan.common.mybatis.core.page.PageQuery;
import com.baichuan.common.core.domain.R;
import com.baichuan.common.core.validate.AddGroup;
import com.baichuan.common.core.validate.EditGroup;
import com.baichuan.common.log.enums.BusinessType;
import com.baichuan.common.excel.utils.ExcelUtil;
import com.baichuan.zntx.domain.vo.DeviceVo;
import com.baichuan.zntx.domain.bo.DeviceBo;
import com.baichuan.zntx.service.IDeviceService;
import com.baichuan.common.mybatis.core.page.TableDataInfo;

/**
 * 智能铁鞋
 *
 * @author Baichuan
 * @date 2024-05-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zntx-app/device")
public class DeviceController extends BaseController {

    private final IDeviceService deviceService;


    @SaCheckPermission("zntx-app:device:list")
    @GetMapping("/listByTask/{type}")
    public R<?> listByTask(@PathVariable("type")Integer type) {
        return R.ok(deviceService.listByTask(type));
    }
    /**
     * 查询智能铁鞋列表
     */
    @SaCheckPermission("zntx-app:device:list")
    @GetMapping("/list")
    public TableDataInfo<DeviceVo> list(DeviceBo bo, PageQuery pageQuery) {
        return deviceService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出智能铁鞋列表
     */
    @SaCheckPermission("zntx-app:device:export")
    @Log(title = "智能铁鞋", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DeviceBo bo, HttpServletResponse response) {
        List<DeviceVo> list = deviceService.queryList(bo);
        ExcelUtil.exportExcel(list, "智能铁鞋", DeviceVo.class, response);
    }

    /**
     * 获取智能铁鞋详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("zntx-app:device:query")
    @GetMapping("/{id}")
    public R<DeviceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(deviceService.queryById(id));
    }

    /**
     * 新增智能铁鞋
     */
    @SaCheckPermission("zntx-app:device:add")
    @Log(title = "智能铁鞋", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DeviceBo bo) {
        return toAjax(deviceService.insertByBo(bo));
    }

    /**
     * 修改智能铁鞋
     */
    @SaCheckPermission("zntx-app:device:edit")
    @Log(title = "智能铁鞋", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DeviceBo bo) {
        return toAjax(deviceService.updateByBo(bo));
    }

    /**
     * 删除智能铁鞋
     *
     * @param ids 主键串
     */
    @SaCheckPermission("zntx-app:device:remove")
    @Log(title = "智能铁鞋", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(deviceService.deleteWithValidByIds(List.of(ids), true));
    }
}
